package com.medvalley.tcm.service.impl;

import com.google.common.collect.Lists;
import com.medvalley.tcm.base.BusinessException;
import com.medvalley.tcm.base.EmptyException;
import com.medvalley.tcm.dao.*;
import com.medvalley.tcm.domain.*;
import com.medvalley.tcm.pojo.*;
import com.medvalley.tcm.service.IDiagnosisDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class DiagnosisDataServiceImpl implements IDiagnosisDataService {

    @Autowired
    private EarDataDao earDataDao;

    @Autowired
    private TongueDao tongueDao;

    @Autowired
    private PulseDao pulseDao;

    @Autowired
    private PulseDataDao pulseDataDao;

    @Autowired
    private SickInfoDao sickInfoDao;

    @Autowired
    private BodyDao bodyDao;

    @Override
    public void saveEarData(EarPojo earPojo) {
        EarData earData = new EarData();
        BeanUtils.copyProperties(earPojo, earData);
        Date now = new Date();
        earData.setCreateTime(now);
        earData.setUpdateTime(now);
        earDataDao.save(earData);
        earPojo.setId(earData.getId());
        Optional<SickInfo> sickInfoOptional = sickInfoDao.findById(earPojo.getMedicalID());
        if (sickInfoOptional.get() != null) {
            sickInfoOptional.get().setEarID(earData.getId());
            sickInfoDao.save(sickInfoOptional.get());
        }
    }

    @Override
    public EarPojo getEarDataById(long id) {
        EarData earData = earDataDao.findById(id).orElse(null);
        if (earData == null) {
            throw new EmptyException("没有获取到相关数据");
        }
        EarPojo earPojo = new EarPojo();
        BeanUtils.copyProperties(earData, earPojo);
        return earPojo;
    }

    @Override
    public List<EarPojo> getEarDataByMedicalID(long sickId) {
        List<EarData> earDataList = earDataDao.findAllByMedicalID(sickId);
        final List<EarPojo> earPojoList = Lists.newArrayList();
        earDataList.forEach(item -> {
            EarPojo earPojo = new EarPojo();
            BeanUtils.copyProperties(item, earPojo);
            earPojoList.add(earPojo);
        });
        return earPojoList;
    }

    @Override
    public void updateEarData(EarPojo earPojo) {
        EarData earData = earDataDao.findById(earPojo.getId()).orElseThrow(() -> new BusinessException("没有此耳朵数据"));

        BeanUtils.copyProperties(earPojo, earData, "createTime", "id");
        Date now = new Date();
        earData.setUpdateTime(now);
        earDataDao.save(earData);
    }

    @Override
    public TonguePojo saveTongue(TonguePojo tonguePojo) {
        Tongue tongue = new Tongue();
        BeanUtils.copyProperties(tonguePojo, tongue);
        Date now = new Date();
        tongue.setCreateTime(now);
        tongue.setUpdateTime(now);
        tongueDao.save(tongue);
        tonguePojo.setId(tongue.getId());
        Optional<SickInfo> sickInfoOptional = sickInfoDao.findById(tonguePojo.getMedicalID());
        if (sickInfoOptional.get() != null) {
            sickInfoOptional.get().setTongueID(tongue.getId());
            sickInfoDao.save(sickInfoOptional.get());
        }
        return tonguePojo;
    }

    @Override
    public TonguePojo getTongueById(long id) {
        Tongue tongue = tongueDao.findById(id).orElse(null);
        if (tongue == null) {
            throw new EmptyException("没有找到相关数据");
        }
        TonguePojo tonguePojo = new TonguePojo();
        BeanUtils.copyProperties(tongue, tonguePojo);
        return tonguePojo;
    }

    @Override
    public List<TonguePojo> getTongueBySickId(long sickId) {
        List<Tongue> tongueList = tongueDao.findAllByMedicalID(sickId);
        final List<TonguePojo> tonguePojoList = Lists.newArrayList();
        tongueList.forEach(item -> {
            TonguePojo tonguePojo = new TonguePojo();
            BeanUtils.copyProperties(item, tonguePojo);
            tonguePojoList.add(tonguePojo);
        });
        return tonguePojoList;
    }

    @Override
    public void updateTongue(TonguePojo tonguePojo) {
        Tongue tongue = tongueDao.findById(tonguePojo.getId()).orElseThrow(() -> new BusinessException("没有此舌像数据"));
        BeanUtils.copyProperties(tonguePojo, tongue, "createTime", "id");
        Date now = new Date();
        tongue.setUpdateTime(now);
        tongueDao.save(tongue);
    }

    @Override
    public PulsePojo savePulse(PulsePojo pulsePojo) {
        Pulse pulse = new Pulse();
        BeanUtils.copyProperties(pulsePojo, pulse);
        Date now = new Date();
        pulse.setCreateTime(now);
        pulse.setUpdateTime(now);
        pulseDao.save(pulse);
        pulsePojo.setId(pulse.getId());
        Optional<SickInfo> sickInfoOptional = sickInfoDao.findById(pulse.getMedicalID());
        if (sickInfoOptional.get() != null) {
            sickInfoOptional.get().setPulseID(pulse.getId());
            sickInfoDao.save(sickInfoOptional.get());
        }
        return pulsePojo;
    }

    @Override
    public PulsePojo getPulseById(long id) {
        Pulse pulse = pulseDao.findById(id).orElse(null);
        if (pulse == null) {
            throw new EmptyException("没有相关脉象数据");
        }
        PulsePojo pulsePojo = new PulsePojo();
        BeanUtils.copyProperties(pulse, pulsePojo);
        List<PulseData> pulseDataList = pulseDataDao.findAllByPulseId(id);
        List<PulseDataPojo> pulseDataPojos = Lists.newArrayList();
        pulseDataList.forEach(item -> {
            PulseDataPojo pulseDataPojo = new PulseDataPojo();
            BeanUtils.copyProperties(item, pulseDataPojo);
            pulseDataPojos.add(pulseDataPojo);
        });
        pulsePojo.setPulseDatas(pulseDataPojos);
        return pulsePojo;
    }

    @Override
    public void updatePulse(PulsePojo pulsePojo) {
        Pulse pulse = pulseDao.findById(pulsePojo.getId()).orElseThrow(() -> new BusinessException("没有此脉搏数据"));
        BeanUtils.copyProperties(pulsePojo, pulse, "createTime", "id");
        Date now = new Date();
        pulse.setUpdateTime(now);
        pulseDao.save(pulse);
    }

    @Override
    public PulseDataPojo savePulseData(PulseDataPojo pulseDataPojo) {
        PulseData pulseData = new PulseData();
        BeanUtils.copyProperties(pulseDataPojo, pulseData);
        Date now = new Date();
        pulseData.setCreateTime(now);
        pulseData.setUpdateTime(now);
        pulseDataDao.save(pulseData);
        pulseDataPojo.setId(pulseData.getId());
        return pulseDataPojo;
    }

    @Override
    public PulseDataPojo updatePulseData(PulseDataPojo pulseDataPojo) {
        PulseData pulseData = pulseDataDao.findById(pulseDataPojo.getId()).orElseThrow(() -> new BusinessException("没有此脉象数据"));
        BeanUtils.copyProperties(pulseDataPojo, pulseData, "createTime","id");
        Date now = new Date();
        pulseData.setUpdateTime(now);
        pulseDataDao.save(pulseData);
        return pulseDataPojo;
    }

    @Override
    public BodyPojo saveBody(BodyPojo bodyPojo) {
        Body body = new Body();
        BeanUtils.copyProperties(bodyPojo, body);
        Date now = new Date();
        body.setCreateTime(now);
        body.setUpdateTime(now);
        bodyDao.save(body);
        bodyPojo.setId(body.getId());
        return bodyPojo;
    }

    @Override
    public BodyPojo getPojoById(long id) {
        Body body = bodyDao.findById(id).orElse(null);
        if (body == null) {
            throw new EmptyException("没有相关数据");
        }
        BodyPojo bodyPojo = new BodyPojo();
        BeanUtils.copyProperties(body, bodyPojo);
        return bodyPojo;
    }

    @Override
    public BodyPojo updateBody(BodyPojo bodyPojo) {
        Date now = new Date();
        Body body = new Body();
        BeanUtils.copyProperties(bodyPojo, body, "createTime");
        body.setUpdateTime(now);
        bodyDao.save(body);
        return bodyPojo;
    }

}
